package com.yeyks.controller;

import com.alibaba.fastjson.JSON;
import com.yeyks.base.Arguments;
import com.yeyks.dal.dao.AccountInfoMapper;
import com.yeyks.dal.dao.HotelInfoMapper;
import com.yeyks.dal.dao.RoomInfoMapper;
import com.yeyks.dal.domain.AccountInfo;
import com.yeyks.dal.domain.HotelInfo;
import com.yeyks.dal.domain.RoomInfo;
import com.yeyks.exception.PermissionInvalidException;
import com.yeyks.exception.ServiceException;
import com.yeyks.exception.TokenInvalidException;
import com.yeyks.model.Ajax.AjaxResponseBody;
import com.yeyks.model.RoomBookingRules;
import com.yeyks.model.em.AccountType;
import com.yeyks.model.em.ResponseStatus;
import com.yeyks.service.AccountService;
import com.yeyks.service.RoomInfoService;
import com.yeyks.util.DateUtils;
import com.yeyks.util.TextValidator;
import com.yeyks.util.Utility;
import com.yeyks.exception.PermissionInvalidException;
import com.yeyks.exception.TokenInvalidException;
import com.yeyks.model.Ajax.AjaxResponseBody;
import com.yeyks.model.RoomBookingRules;
import com.yeyks.model.em.AccountType;
import com.yeyks.model.em.ResponseStatus;
import com.yeyks.service.AccountService;
import com.yeyks.util.Utility;
import org.omg.CORBA.DynAnyPackage.Invalid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.InvalidParameterException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ajax/room")
public class RoomController {
    @Autowired
    private AccountInfoMapper accountInfoMapper;

    @Autowired
    private AccountService accountService;

    @Autowired
    private HotelInfoMapper hotelInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private RoomInfoService roomInfoService;

    /**
     * 查询酒店房间信息
     * @param request
     * @param response
     * @param token
     * @param id 若ID不填，则查询 token 对应
     * @return
     * @throws Exception
     */
    @ApiIgnore
    @RequestMapping(value = "/searchRoom", method = RequestMethod.POST)
    public ResponseEntity<?> searchRoom(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            @RequestParam(name = "id", defaultValue = "0", required = true) Integer id
    ) throws Exception {

        AjaxResponseBody responseBody = new AjaxResponseBody();

        try {

            if (id == 0) {
                AccountInfo accountInfo = accountService.token2AccountInfo(token);
                if (accountInfo.getType() == AccountType.HotelManager) {
                    HotelInfo hotelInfo = accountService.searchHotelByOperator(accountInfo.getId());
                    if (hotelInfo == null) {
                        responseBody.setCode(ResponseStatus.RS_ROOM_NO_HOTEL_INFO.getCode());
                        responseBody.setMsg(ResponseStatus.RS_ROOM_NO_HOTEL_INFO.getMsg());
                    } else {
                        id = hotelInfo.getId();
                    }
                } else {
                    responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_PARAMETERS.getCode());
                    responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_PARAMETERS.getMsg());
                }
            }

            if (id != 0) {
                List<RoomInfo> roomInfoList = roomInfoMapper.selectByHotelId(id);

                responseBody.setBody(roomInfoList);
                responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
            }
        } catch (TokenInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_TOKEN.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_TOKEN.getMsg());
        }  catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL.getMsg());
        }

        return ResponseEntity.ok(responseBody);
    }

    /**
     * 增加房型
     * @param request
     * @param response
     * @param token
     * @param roomInfo
     * @return
     * @throws Exception
     */
    @ApiIgnore
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseEntity<?> add(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            RoomInfo roomInfo
    ) throws Exception {

        AjaxResponseBody responseBody = new AjaxResponseBody();
        checkout(roomInfo);
        try {
            AccountInfo operatorInfo = accountService.token2AccountInfo(token);
//            if (operatorInfo.getType() > AccountType.HotelManager) {
            if (operatorInfo.getType() == AccountType.User) {
                throw new PermissionInvalidException();
            }

            // 查询酒店管理员所对应管理的酒店信息
            HotelInfo hotelInfo = accountService.searchHotelByOperator(token);
            if (hotelInfo != null) {

                // 查询房间是否已存在
                List<RoomInfo> roomInfoList = roomInfoMapper.selectByHotelIdAndTitle(hotelInfo.getId(), roomInfo.getTitle());
                if (roomInfoList == null || roomInfoList.size() == 0) {
                    if (roomInfo.getCount() == null) {
                        roomInfo.setCount(0);
                    }
                    roomInfo.setHotelId(hotelInfo.getId());
                    roomInfo.setIsDel(1);
                    roomInfo.setAvailable(roomInfo.getCount());
                    roomInfo.setTodayTime(DateUtils.localDateToDate(LocalDate.now()));
                    roomInfoMapper.insertReturnId(roomInfo);
                    responseBody.setBody(roomInfo);

                    responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                    responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
                } else {

                    responseBody.setBody(roomInfoList);
                    responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
                    responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
                }
            } else {
                responseBody.setCode(ResponseStatus.RS_ROOM_NO_HOTEL_INFO.getCode());
                responseBody.setMsg(ResponseStatus.RS_ROOM_NO_HOTEL_INFO.getMsg());
            }
        } catch (TokenInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_TOKEN.getCode());
            responseBody.setMsg(e.getMessage());
        } catch (PermissionInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_PERMISSION.getCode());
            responseBody.setMsg(e.getMessage());
        } catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(e.getMessage());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(e.getMessage());
        }

        return ResponseEntity.ok(responseBody);
    }

    private void checkout(RoomInfo roomInfo) {
        if (Arguments.notEmpty(roomInfo.getTitle()) && roomInfo.getTitle().length() >= 30) {
            throw new ServiceException("标题太长，请控制在30字内");
        }
        if (Arguments.isNullOrZero(roomInfo.getCurPrice())
                || roomInfo.getCurPrice() >= 1000000) {
            throw new ServiceException("售价输入非法");
        }
        if (Arguments.isNullOrZero(roomInfo.getMaxPeople())
                || roomInfo.getMaxPeople() > 99) {
            throw new ServiceException("入住人数输入非法");
        }
        if (roomInfo.getFloor() != null && roomInfo.getFloor() > 1000 ) {
            throw new ServiceException("楼层数不能大于1000");
        }
        if (roomInfo.getCount() != null && roomInfo.getCount() > 1000 ) {
            throw new ServiceException("请输入合适的房间数");
        }
    }

    /**
     * 修改房间的上架状态
     * @param request
     * @param response
     * @param token
     * @param roomIds
     * @param valid
     * @return
     * @throws Exception
     */
    @ApiIgnore
    @RequestMapping(value = "/updateValid", method = RequestMethod.POST)
    public ResponseEntity<?> updateValid(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            @RequestParam(name = "roomIds", defaultValue = "", required = true) String roomIds,
            @RequestParam(name = "valid", defaultValue = "true", required = true) Boolean valid

    ) throws Exception {

        AjaxResponseBody responseBody = new AjaxResponseBody();

        try {
            AccountInfo operatorInfo = accountService.token2AccountInfo(token);

            List<Integer> ids = Utility.str2intList(roomIds);

            if (!accountService.roomHavePermission(token, ids)) {
                responseBody.setCode(ResponseStatus.RS_ROOM_NOT_MATCH_HOTEL.getCode());
                responseBody.setMsg(ResponseStatus.RS_ROOM_NOT_MATCH_HOTEL.getMsg());
            } else {

                roomInfoMapper.updateRoomValid(ids, valid);

                responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
            }

        } catch (TokenInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_TOKEN.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_TOKEN.getMsg());
        }  catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL.getMsg());
        }

        return ResponseEntity.ok(responseBody);
    }

    /**
     * 获取酒店的退款配置信息
     * @param request
     * @param response
     * @param roomInfo
     * @return
     * @throws Exception
     */
    @ApiIgnore
    @RequestMapping(value = "/getRefundCfg", method = RequestMethod.POST)
    public ResponseEntity<?> getRefundCfg(
            HttpServletRequest request, HttpServletResponse response,
            RoomInfo roomInfo
    ) throws Exception {
        AjaxResponseBody responseBody = new AjaxResponseBody();
        try {
            if (roomInfo.getHotelId() == null || roomInfo.getHotelId() == 0) {
                if (roomInfo.getId() == null || roomInfo.getId() == 0) {
                    throw new InvalidParameterException("invalid parameters id == 0");
                }

                roomInfo = roomInfoMapper.selectByPrimaryKey(roomInfo.getId());
                if (roomInfo == null) {
                    throw new InvalidParameterException("cannot find room information");
                }
            }

            if (roomInfo.getHotelId() == null || roomInfo.getHotelId() == 0) {
                throw new InvalidParameterException("invalid parameters hotelId == 0");
            }

            HotelInfo hotelInfo = hotelInfoMapper.selectByPrimaryKey(roomInfo.getHotelId());
            if (hotelInfo == null) {
                throw new InvalidParameterException("cannot find hotel information");
            }

            responseBody.setBody(hotelInfo.getRefundCfg());
            responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
            responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
        } catch (InvalidParameterException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_PARAMETERS.getCode());
            responseBody.setMsg(e.getMessage());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL.getMsg());
        }

        return ResponseEntity.ok(responseBody);
    }

    /**
     * 修改房间信息
     * @param request
     * @param response
     * @param token
     * @param roomInfo
     * @return
     * @throws Exception
     */
    @ApiIgnore
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public ResponseEntity<?> modify(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            RoomInfo roomInfo
    ) throws Exception {

        AjaxResponseBody responseBody = new AjaxResponseBody();

        try {
            AccountInfo accountInfo = accountService.token2AccountInfo(token);
//            if (accountInfo.getType() > AccountType.HotelManager) {
            if (accountInfo.getType() == AccountType.User) {
                throw new PermissionInvalidException();
            } else if (accountInfo.getType() == AccountType.HotelManager) {
                RoomInfo origRoomInfo = roomInfoMapper.selectByPrimaryKey(roomInfo.getId());
                HotelInfo hotelInfo = accountService.searchHotelByOperator(token, origRoomInfo.getHotelId());

                if (hotelInfo == null) {
                    responseBody.setCode(ResponseStatus.RS_ROOM_HOTEL_ID_NE.getCode());
                    responseBody.setMsg(ResponseStatus.RS_ROOM_HOTEL_ID_NE.getMsg());
                } else {
                    if (origRoomInfo.getTodayTime() != null) {
                        roomInfo.setTodayTime(origRoomInfo.getTodayTime());
                    }
//                    roomInfo.setAvailable(roomInfo.getCount());
                    roomInfoMapper.updateByPrimaryKey(roomInfo);
                    responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                    responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
                }
            } else {
                RoomInfo origRoomInfo = roomInfoMapper.selectByPrimaryKey(roomInfo.getId());
                roomInfo.setHotelId(origRoomInfo.getHotelId());
                if (origRoomInfo.getTodayTime() != null) {
                    roomInfo.setTodayTime(origRoomInfo.getTodayTime());
                }
//                roomInfo.setAvailable(roomInfo.getCount());
                roomInfoMapper.updateByPrimaryKey(roomInfo);
                responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
            }
        } catch (TokenInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_TOKEN.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_TOKEN.getMsg());
        }  catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL.getMsg());
        }

        return ResponseEntity.ok(responseBody);
    }

    public void putExtraInfo(
            Integer roomId, String key, String value
    ) throws Exception {
        RoomInfo origRoomInfo = roomInfoMapper.selectByPrimaryKey(roomId);
        if (origRoomInfo == null) {
            throw new InvalidParameterException();
        }

        String extraInfo = Utility.setExtraInfo(origRoomInfo.getExtraInfo(), key, value);
        int rec = roomInfoMapper.updateExtraInfo(roomId, extraInfo);

        if (rec == 0) {
            throw new Exception("Updated 0 rows");
        }
    }

    public String getExtraInfo(Integer roomId, String key) {
        RoomInfo origRoomInfo = roomInfoMapper.selectByPrimaryKey(roomId);
        if (origRoomInfo == null) {
            throw new InvalidParameterException();
        }
        String rule = Utility.getExtraInfo(origRoomInfo.getExtraInfo(), key);
        return Arguments.isEmpty(rule) ? "{}" : rule;
    }

    /**
     * 设置房间的开放时间规则
     * @param request
     * @param response
     * @param token
     * @param roomId
     * @param rule
     * @param available
     * @return
     * @throws Exception
     */
    @ApiIgnore
    @RequestMapping(value = "/setBookingRule", method = RequestMethod.POST)
    public ResponseEntity<?> setBookingRule(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            @RequestParam(name = "roomId", defaultValue = "", required = true) Integer roomId,
            @RequestParam(name = "rule", defaultValue = "", required = true) String rule,
            @RequestParam(name = "available", defaultValue = "", required = true) Integer available
    ) throws Exception {

        AjaxResponseBody responseBody = new AjaxResponseBody();

        if (!Arguments.notEmpty(rule)) {
            throw new ServiceException("房间开放规则不能为空");
        }
        try {

            AccountInfo accountInfo = accountService.token2AccountInfo(token);
            if (accountInfo.getType() != AccountType.HotelManager) {
                throw new PermissionInvalidException();
            } else {

                RoomBookingRules bookingRules = JSON.parseObject(rule, RoomBookingRules.class);
                if (bookingRules == null) {
                    throw new InvalidParameterException("booking rule error");
                }

                RoomInfo origRoomInfo = roomInfoMapper.selectByPrimaryKey(roomId);
                if (origRoomInfo == null) {
                    throw new InvalidParameterException("cannot find room information.");
                }

                String extraInfo = Utility.setExtraInfo(origRoomInfo.getExtraInfo(), "booking_rule", rule);
                RoomInfo roomInfo = new RoomInfo();
                roomInfo.setId(roomId);
                roomInfo.setExtraInfo(extraInfo);
                roomInfo.setAvailable(available);
                int rec = roomInfoMapper.updateIfNotNullByPrimaryKey(roomInfo);

                if (rec == 0) {
                    throw new Exception("Updated 0 rows");
                }

                responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
            }
        } catch (TokenInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_TOKEN.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_TOKEN.getMsg());
        } catch (InvalidParameterException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_PARAMETERS.getCode());
            responseBody.setMsg(e.getMessage());
        } catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(e.getMessage());
        }

        return ResponseEntity.ok(responseBody);
    }

    /**
     * 获取房间的开放时间规则
     * @param request
     * @param response
     * @param token
     * @param roomId
     * @return
     * @throws Exception
     */
    @ApiIgnore
    @RequestMapping(value = "/getBookingRule", method = RequestMethod.POST)
    public ResponseEntity<?> getBookingRule(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            @RequestParam(name = "roomId", defaultValue = "", required = true) Integer roomId
    ) throws Exception {

        AjaxResponseBody responseBody = new AjaxResponseBody();

        try {

            AccountInfo accountInfo = accountService.token2AccountInfo(token);
            if (accountInfo.getType() != AccountType.HotelManager) {
                throw new PermissionInvalidException();
            } else {

                responseBody.setBody(getExtraInfo(roomId, "booking_rule"));
                responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
            }
        } catch (TokenInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_TOKEN.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_TOKEN.getMsg());
        }  catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(e.getMessage());
        }

        return ResponseEntity.ok(responseBody);
    }

    /**
     * 增加房间的扩展信息
     * @param request
     * @param response
     * @param token
     * @param roomId
     * @param key
     * @param value
     * @return
     * @throws Exception
     */
    @ApiIgnore
    @RequestMapping(value = "/putExtraInfo", method = RequestMethod.POST)
    public ResponseEntity<?> putExtraInfo(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            @RequestParam(name = "roomId", defaultValue = "", required = true) Integer roomId,
            @RequestParam(name = "key", defaultValue = "", required = true) String key,
            @RequestParam(name = "value", defaultValue = "", required = true) String value
    ) throws Exception {

        AjaxResponseBody responseBody = new AjaxResponseBody();

        try {

            AccountInfo accountInfo = accountService.token2AccountInfo(token);
            if (accountInfo.getType() != AccountType.HotelManager) {
                throw new PermissionInvalidException();
            } else {
                putExtraInfo(roomId, key, value);

                responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
            }
        } catch (TokenInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_TOKEN.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_TOKEN.getMsg());
        }  catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(e.getMessage());
        }

        return ResponseEntity.ok(responseBody);
    }

    /**
     * 获取房间的扩展信息
     * @param request
     * @param response
     * @param token
     * @param roomId
     * @param key
     * @return
     * @throws Exception
     */
    @ApiIgnore
    @RequestMapping(value = "/getExtraInfo", method = RequestMethod.POST)
    public ResponseEntity<?> getExtraInfo(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            @RequestParam(name = "roomId", defaultValue = "", required = true) Integer roomId,
            @RequestParam(name = "key", defaultValue = "", required = true) String key
    ) throws Exception {

        AjaxResponseBody responseBody = new AjaxResponseBody();

        try {

            AccountInfo accountInfo = accountService.token2AccountInfo(token);
            if (accountInfo.getType() != AccountType.HotelManager) {
                throw new PermissionInvalidException();
            } else {
                responseBody.setBody(getExtraInfo(roomId, key));

                responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
            }
        } catch (TokenInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_TOKEN.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_TOKEN.getMsg());
        }  catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(e.getMessage());
        }

        return ResponseEntity.ok(responseBody);
    }

    /**
     * 删除房间信息
     * @param request
     * @param response
     * @param token
     * @param roomInfo
     * @return
     * @throws Exception
     */
    @ApiIgnore
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ResponseEntity<?> del(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            RoomInfo roomInfo
    ) throws Exception {

        AjaxResponseBody responseBody = new AjaxResponseBody();

        try {

            AccountInfo accountInfo = accountService.token2AccountInfo(token);
//            if (accountInfo.getType() > AccountType.HotelManager) {
            if (accountInfo.getType() == AccountType.User) {
                throw new PermissionInvalidException();
            } else if (accountInfo.getType() == AccountType.HotelManager) {
                RoomInfo origRoomInfo = roomInfoMapper.selectByPrimaryKey(roomInfo.getId());
                HotelInfo hotelInfo = accountService.searchHotelByOperator(token, origRoomInfo.getHotelId());

                if (hotelInfo == null) {
                    responseBody.setCode(ResponseStatus.RS_ROOM_HOTEL_ID_NE.getCode());
                    responseBody.setMsg(ResponseStatus.RS_ROOM_HOTEL_ID_NE.getMsg());
                } else {
//                    roomInfoMapper.deleteByPrimaryKey(roomInfo.getId());
                    roomInfoService.deleteById(roomInfo.getId(), 0);
                    responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                    responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
                }
            } else {
//                roomInfoMapper.deleteByPrimaryKey(roomInfo.getId());
                roomInfoService.deleteById(roomInfo.getId(), 0);
                responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
            }
        } catch (TokenInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_TOKEN.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_TOKEN.getMsg());
        }  catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL.getMsg());
        }

        return ResponseEntity.ok(responseBody);
    }
}
